<?php

class typeAction extends commonAction{
    function _initialize() {
        parent::_initialize();
        helper('string');//字符串处理助手
        helper('tree');//树生成类
        helper("arcfunc");//内容管理助手
    }
    //栏目列表页
    function index(){
        $ArctypeM=D('Arctype');
        $list=$ArctypeM->GetMoveOptions();
        $this->assign('list',$list);
        $this->display();
    }
    
    //添加栏目
   function add(){
       $ArctypeM=D('Arctype');
       if($this->isPost()){
           if($ArctypeM->create()){
               if($ArctypeM->add()){
                   $this->success("栏目添加成功");
               }else{
                   $this->error($ArctypeM->getDbError());
               }
           }else{
               $this->error($ArctypeM->getError());
           }
       }else{
           $id=I('id',0);//上级栏目id
           if($id>0){
               $ArctypeM_re=$ArctypeM->find($id);
           }
           $ptype=$ArctypeM_re?$ArctypeM_re:array();
           //读取频道信息
           $channels=M('Channel')->field(true)->select();
           if($channels===false){
               $this->error("读取频道信息失败!");
           }
           //读取栏目列表
           $typelist=$ArctypeM->GetMoveOptions();
           $this->assign("ptype",$ptype);
           $this->assign("typelist",$typelist);
           $this->assign("channels",$channels);
           $this->display();
       }
   }
   
   //修改栏目设置
   function edit(){
       $ArctypeM=D('Arctype');
       $typeid=I('id','0');
       if($this->isPost()){
           
           if($ArctypeM->create()){
               if($ArctypeM->save()!==false){
                   $this->success("栏目修改成功",U('index'));
               }else{
                   $this->error($ArctypeM->getDbError());
               }
           }else{
               $this->error($ArctypeM->getError());
           }
       }else{
           //读取频道信息
           $channels=M('Channel')->field(true)->select();
           if($channels===false){
               $this->error("读取频道信息失败!");
           }
           //读取栏目列表
           $typelist=$ArctypeM->field(true)->find($typeid);
           if($typelist==FALSE){
               $this->error('栏目不存在');
           }
           $moveoptions=$ArctypeM->GetMoveOptions($typeid);
           $this->assign("moveoptions",$moveoptions);
           $this->assign("list",$typelist);
           $this->assign("channels",$channels);
           $this->display();
       }
   }
   
   //删除分类
   function delete(){
       $typeid = I("id", ""); //获取分类id
        if (empty($typeid) || !is_numeric($typeid)) {
            $this->error("分类id不合法");
        }
        $arctypeM=D("Arctype");
        if (!$arctypeM->delType($typeid)) {//此处调用助手arcfunc中的函数
            $this->error("删除分类失败，分类id：{$typeid}".$arctypeM->getError());
        }
        $this->success("删除成功");
   }
}
